﻿using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace ProjectWebNhac.DAO
{
    public class Dataprovider
    {
        
        static SqlConnection cn = null;
        static SqlDataAdapter da = null;
        SqlCommand cmd = null;
        DataTable dt = null;
        string query = @"Data Source=bjnsunny;Initial Catalog=DB_projectwebnhac;Integrated Security=True";
        public Dataprovider()
        {

        }

        public DataTable LayDuLieuThamSo(string sql, CommandType command, params object[] par)
        {
            dt = new DataTable();
            cn = new SqlConnection(query);
            cmd = new SqlCommand(sql, cn);
            cmd.CommandType = command;
            for (int i = 0; i < par.Length; i += 2)
            {
                SqlParameter para = new SqlParameter(par[i].ToString(), par[i + 1]);
                cmd.Parameters.Add(para);
            }
            da = new SqlDataAdapter(cmd);
            dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        public bool XuLySuKien(string sql, CommandType command, params object[] par)
        {
            cn = new SqlConnection(query);
            int numRecordsEffect = 0;
            try
            {
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = command;
                for (int i = 0; i < par.Length; i += 2)
                {
                    SqlParameter para = new SqlParameter(par[i].ToString(), par[i + 1]);
                    cmd.Parameters.Add(para);
                }
                numRecordsEffect = cmd.ExecuteNonQuery();
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
            if (numRecordsEffect > 0)
                return true;
            return false;
        }
        public DataSet LayDsThamSo(string sql, CommandType command, params object[] par)
        {
            DataSet ds = new DataSet();
            cn = new SqlConnection(query);
            cmd = new SqlCommand(sql, cn);
            cmd.CommandType = command;
            for (int i = 0; i < par.Length; i += 2)
            {
                SqlParameter para = new SqlParameter(par[i].ToString(), par[i + 1]);
                cmd.Parameters.Add(para);
            }
            da = new SqlDataAdapter(cmd);
          
            da.Fill(ds);
            return ds;
        }
    }
}